/**     
* @Title: ReptileData.java   
* @Package me.crawler.init
* @Description: TODO   
* @author xaoyaoyao
* @date 2019年1月19日 下午3:38:09
*/
package me.crawler.init;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import lombok.extern.slf4j.Slf4j;
import me.crawler.entity.SpiderSeed;
import me.crawler.entity.SpiderTask;
import me.crawler.jedis.RedisService;
import me.crawler.util.Constants;
import me.crawler.util.ContextLoader;

/**
 * @ClassName: ReptileData
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2019年1月19日 下午3:38:09
 * 
 */
@Slf4j
public class ReptileData {

	private static final String SEPARATOR = ";";

	private final static int FIFTEEN_MINUTES = 30 * 60;

	private static RedisService redisService;

	private static void init() {
		redisService = null == redisService ? ContextLoader.getBean(RedisService.class) : redisService;
	}

	public static void initSpiderSeeds() {
		log.debug(">>>>>>>> initSpiderSeeds");
		init();
		// TODO 数据获取，然后写入reids
		List<SpiderSeed> seeds = new ArrayList<SpiderSeed>();
		if (CollectionUtils.isNotEmpty(seeds)) {
			Map<String, String> hash = new ConcurrentHashMap<String, String>();
			for (SpiderSeed seed : seeds) {
				hash.put(seed.getId() + "", seed.toString());
			}
			redisService.hmset(Constants.SPIDER_SEED_KEYS, hash, FIFTEEN_MINUTES);
		}
	}

	@SuppressWarnings("unchecked")
	public static Map<String, List<SpiderSeed>> getSpiderSeeds() {
		init();
		// 获取种子(可将种子放入缓存中，比如redis，key是domain值)
		Map<String, String> data = redisService.hmget(Constants.SPIDER_SEED_KEYS);
		if (MapUtils.isEmpty(data)) {
			initSpiderSeeds();
			data = redisService.hmget(Constants.SPIDER_SEED_KEYS);
		}
		if (MapUtils.isNotEmpty(data)) {
			Map<String, List<SpiderSeed>> map = new ConcurrentHashMap<String, List<SpiderSeed>>();
			data.forEach((k, v) -> {
				try {
					SpiderSeed seed = SpiderSeed.parse(v);
					if (null != seed) {
						String domail = seed.getDomain();
						if (StringUtils.isNotBlank(domail)) {
							if (domail.contains(SEPARATOR)) {
								String[] arr = domail.split(SEPARATOR);
								if (null != arr && arr.length > 0) {
									for (String str : arr) {
										put(str, seed, map);
									}
								}
							} else {
								put(domail, seed, map);
							}
						}
					}
				} catch (Exception e) {
					log.error(e.getMessage(), e);
				}
			});
			return map;
		}
		return new HashMap<String, List<SpiderSeed>>(1);
	}

	private static void put(String domail, SpiderSeed seed, Map<String, List<SpiderSeed>> map) {
		List<SpiderSeed> list = map.get(domail);
		if (CollectionUtils.isEmpty(list)) {
			list = new ArrayList<SpiderSeed>();
		}
		list.add(seed);
		map.put(domail, list);
	}

	public static SpiderTask getGrabTask() {
		try {
			init();
			// 获取任务，可从redis获取任务
			String json = redisService.rpop(Constants.SPIDER_TASK_KEYS);
			log.debug(">>>>>>>> SpiderTask >> {}", json);
			if (StringUtils.isNotBlank(json)) {
				return SpiderTask.parse(json);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}
}
